<?php
	/*
		PandaForum: PHP Forum Engine
		
		Jesse Gill, 0108377707
	*/
	
	require_once('./src/classes/Page.php');
	require('./src/config.php');
	
	$page = new Page('Viewtopic');
	
	$page->print_header();
	
	$id =					$page->connection->clean_var($_GET['id']);
	$pagenum = 				$page->connection->clean_var($_GET['page']);
	
	$topic = $page->connection->get_result_array("SELECT * FROM topic WHERE id=$id");
	
	if (empty($topic))
		die("Invalid topic id.");
	
	$result = $page->connection->get_result("SELECT * FROM post WHERE topicid=$id");
	
	if (! $result)
	{
		$numpages = 1;
	}
	else
	{
		if ($result->num_rows == 0)
		{
			$numpages = 1;
		}
		else
		{
			$numpages = ($result) ? ceil($result->num_rows / $postsperpage) : 1;
		}
	}
	
	if ($result)
		$result->free();
?>
	<link href='src/css/viewtopic.css' rel='stylesheet' type='text/css' />
	<script type="text/javascript" src="src/js/ajax_functions_viewtopic.js"></script>
	
	<script type="text/javascript">
		viewtopic_posts_refreshlist(<?php echo $id; ?>, <?php echo $pagenum; ?>);
	</script>
	
	<h1 class="pageheading">Viewtopic - <?php echo $topic['name']; ?></h1>
	
	<div id="posts">
		
	</div>
	
	<form id="addpost" name="addPost" method="post" action="javascript:ajax_addpost(document.addPost.post.value, <?php echo $topic['forumid']; ?>, <?php echo $topic['id']; ?>, <?php echo $numpages; ?>)">
		<h3>Add Post</h3><br />
		Post: <textarea name='post' rows="10" cols="30"></textarea><br /><br />
		<input type="submit" value="Add Post" /><br /><br />
		<div id="result"> </div>
	</form>
<?php
	$page->print_footer();
?>